var getEditDialog;
var delTemplate;
var $editDialog;
require(['grid', 'topBar', 'layout'], function () {

    $('#top').topBar({
        left: [{
                text: '摸板名称',
                name: 'airTemplateName',
                type: 'text'
            },
            {
                text: '查询',
                type: 'btn',
                click: function () {
                	search();
                }
            }
        ],
        right: [
            {
                text: '+添加模版',
                type: 'btn',
                authority: '',
                click: function () {
                	getEditDialog(0,'添加','');
                }
            }
        ]
    });

    $('body').layout();

    $('#grid').grid({
    	url : "/airConditioner/searchAirTemplatePage.htm",
		colModel:[
			{name: "vrsnId", label: "vrsnId", key: true, hidden: true},
			{name: "vrsnName", label: "模版名称"},
			{name: "acSt", label: "A/C状态",
				formatter:function (cellvalue, options, rowObject){
					if(rowObject.acSt=="1"){
						return" <em style='color: red'>开启</em>"
					}else if(rowObject.acSt=="0"){
						return" <em>关闭</em>"
					}
				}},
			{name: "rcrdTept", label: "设定温度(°C)", align : "right"},
			{name: "todo", label: "操作", width:120, formatter: 'operArea',align: "center",
            formatoptions: [
                {
                    text: '查看',
                    css:'operate-scan',
                    click: function (data) {
                        getEditDialog(1, '查看', data.vrsnId);
                    }
                },
                {
                    text: '编辑',
                    css:'operate-edit',
                    rule: function (data) {
                        return data.beenApplied == "0"
                    },
                    click: function (data) {
                        getEditDialog(2, '编辑', data.vrsnId);
                    }
                },
                {
                    text: '删除',
                    css:'operate-delete',
                    rule: function (data) {
                        return data.beenApplied == "0"
                    },
                    click: function (data) {
                        delTemplate(data.vrsnId);
                    }
                },
				{
					text: '下发',
					css:'operate-issued',
					click: function (data) {
						var type = commonFun.getLeftTreeCurrentTreeType();
						var orgIds = commonFun.getLeftTreeCheckedNodes(type);
						if(type != 'CAR'){
							$.messager.alert("提示","请切换到车辆。","info");
						}else if(!orgIds){
							$.messager.alert("提示","请选择查询车辆。","info");
						}else {
							$.ajaxExt({
								url: "/airConditioner/setAirTemplateForCar.htm",
								data: {'vrsnId': data.vrsnId,'orgIds':orgIds}
							}).done(function (data) {
								$.openTab('486af666d07f409aa4a6231ef3ccc0eb', 'autoSearch');
							});
						}
					}
				}]
			}],
        multiselect: false
    });

    getEditDialog= function(edit,title,vrsnId){
    	if(!$editDialog){
		        $editDialog = $('<div>').dialog({
		            url: './mainte/setting/TemplateAirConditioEdit_at.htm',
			        height: 500,
			        width: 720,
			        close:function(){
			        	$(this).dialog('exec', 'reset',true);
			        },
		            buttons: [
		          	{
						text : '确定',
						css : 'mr10',
						click : function() {
							$(this).dialog('exec', 'saveOrUpdate',function(){
		         		    	$editDialog.dialog('close');
		                    	search();
		        		    });
						}
					}, {
						text : '重置',
						css : 'mr10',
						click : function() {
							$(this).dialog('exec', 'reset');
						}
					}, {
						text : '关闭',
						css : 'btn-cancel',
						click : function() {
							$(this).dialog('close');
						}
					}
		          ]
		    });	
    	}
    	
    	$editDialog.dialog('setOption','title',title);
        
    	//添加
    	if(edit==0){
    		$editDialog.dialog('setOption','showBtn',[0,1,2]);
    	}
    	//查看
    	else if(edit==1){
    		$editDialog.dialog('setOption','showBtn',[2]);
			reloadTemplate(vrsnId);
		}
    	//编辑
    	else if(edit==2){
    		$editDialog.dialog('setOption','showBtn',[0,1,2]);
			reloadTemplate(vrsnId);
		}
    	$editDialog.dialog('open');
    }   

	//重新加载空调模板
	function reloadTemplate(vrsnId){
		$editDialog.dialog('exec', 'reload',vrsnId);
	};
	
	//删除
	delTemplate = function(vrsnId){
		$.messager.confirm("是否删除","是否删除该模板信息？",function(r){
			if(r){
				$.ajaxExt({
					url : "/airConditioner/delAirTemplate.htm",
					data : {'vrsnId' : vrsnId}
				}).done(function(data){
					search();
				});
			}
		});
	};
	
	//查询
	function search(){
		var $postData=$('#top').getValues();
		$postData.appUpgrade = "AIRC0004";
    	$('#grid').grid('setGridParam', {page: 1,datatype: "json", postData : $postData}).trigger("reloadGrid");
	}

    //进入页面自动查询
    search();
    
})
